package cn.iocoder.an.module.business.service.equipmentcheckplan;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.business.dal.dataobject.equipmentmanage.EquipmentManageDO;
import cn.iocoder.an.module.business.dal.dataobject.equipmenttype.EquipmentTypeDO;
import cn.iocoder.an.module.business.dal.mysql.equipmentmanage.EquipmentManageMapper;
import cn.iocoder.an.module.business.dal.mysql.equipmenttype.EquipmentTypeMapper;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckplan.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckplan.EquipmentCheckPlanDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.equipmentcheckplan.EquipmentCheckPlanMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 点检计划 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class EquipmentCheckPlanServiceImpl implements EquipmentCheckPlanService {

    @Resource
    private EquipmentCheckPlanMapper equipmentCheckPlanMapper;

    @Resource
    private EquipmentManageMapper equipmentManageMapper;

    @Resource
    private EquipmentTypeMapper equipmentTypeMapper;

    @Override
    public Long createEquipmentCheckPlan(EquipmentCheckPlanSaveReqVO createReqVO) {
        // 插入
        setOtherData(createReqVO);
        EquipmentCheckPlanDO equipmentCheckPlan = BeanUtils.toBean(createReqVO, EquipmentCheckPlanDO.class);
        equipmentCheckPlanMapper.insert(equipmentCheckPlan);

        // 返回
        return equipmentCheckPlan.getId();
    }

    @Override
    public void updateEquipmentCheckPlan(EquipmentCheckPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentCheckPlanExists(updateReqVO.getId());
        setOtherData(updateReqVO);
        // 更新
        EquipmentCheckPlanDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentCheckPlanDO.class);
        equipmentCheckPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentCheckPlan(Long id) {
        // 校验存在
        validateEquipmentCheckPlanExists(id);
        // 删除
        equipmentCheckPlanMapper.deleteById(id);
    }

    @Override
    public void deleteEquipmentCheckPlanListByIds(List<Long> ids) {
        // 删除
        equipmentCheckPlanMapper.deleteByIds(ids);
    }


    private void validateEquipmentCheckPlanExists(Long id) {
        if (equipmentCheckPlanMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_CHECK_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentCheckPlanDO getEquipmentCheckPlan(Long id) {
        return equipmentCheckPlanMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentCheckPlanDO> getEquipmentCheckPlanPage(EquipmentCheckPlanPageReqVO pageReqVO) {
        return equipmentCheckPlanMapper.selectPage(pageReqVO);
    }

    public void setOtherData(EquipmentCheckPlanSaveReqVO vo) {

        List<Long> equipmentTypeIds = vo.getEquipmentTypeId();
        if (equipmentTypeIds != null && !equipmentTypeIds.isEmpty()) {
            LambdaQueryWrapperX<EquipmentTypeDO> wrapper = new LambdaQueryWrapperX<>();
            wrapper.in(EquipmentTypeDO::getId, equipmentTypeIds);
            List<EquipmentTypeDO> equipmentTypeDOS = equipmentTypeMapper.selectList(wrapper);

            String names = equipmentTypeDOS.stream()
                    .map(EquipmentTypeDO::getName)                    // 提取 name
                    .filter(name -> name != null && !name.trim().isEmpty()) // 过滤 null/空
                    .collect(Collectors.joining(","));
            vo.setEquipmentTypeName(names);

        }

    }

}